package com.hnepri.fang.ftx.fetch.residentialDetail;

import com.hnepri.fang.ftx.model.Residential;
import com.hnepri.fang.ftx.model.ResidentialDetail;
import com.hnepri.util.FileUtil;

import java.io.*;
import java.util.List;

/**
 * 线程对象分区解析
 */
public class ResidentialDetailThread extends Thread{
    private int start;
    private int end;
    private List<Residential> ls;
    String storePath;
    public ResidentialDetailThread(String storePath, List<Residential> ls, int start, int end) {
        this.storePath = storePath;
        this.setName("thead-" + start + "-" + end);
        this.ls = ls;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        int i = 0;
        String url = "";
        try {
            for (i = start; i <= end; i++) {
                String path = storePath + "/" + i + ".txt";
                System.out.println("开始获取第" + i + "个小区...");
                Residential obj = ls.get(i);
                String id = obj.getId();
                url = obj.getDomain();
                if (url == null || url.trim().equals("") || url.indexOf("http") != 0) {
                    continue;
                }
                //开始组合明细页面url
                char lastChar = url.charAt(url.length() - 1);
                if (lastChar != '/') {
                    url = url + "/";
                }
                String esf = url.substring(url.length() - 5);
                if (esf.equals("/esf/")) {
                    url = url.substring(0, url.length() - 4);
                }
                url = url + "xiangqing/";

                StringBuilder sb = new StringBuilder();
                String temp = null;
                try {
                    temp = new ResidentialDetailPage(id, url).getPage();
                    FileUtil.writeFile(path, temp);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            //Thread.currentThread().interrupt();
        }

    }

}
